package com.huawei.secure.android.common.intent;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.io.Serializable;
import java.util.ArrayList;

public class IntentUtils {
    private static final String TAG = "IntentUtils";
    private static final int f = 750000;

    public static boolean safeHasExtra(Intent intent, String str) {
        try {
            return intent.hasExtra(str);
        } catch (Throwable th) {
            return false;
        }
    }

    public static void safeRemoveExtra(Intent intent, String str) {
        try {
            intent.removeExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "removeExtra failed on intent " + th.getMessage());
        }
    }

    public static boolean safeGetBooleanExtra(Intent intent, String str, boolean z) {
        try {
            z = intent.getBooleanExtra(str, z);
        } catch (Throwable th) {
            Log.e(TAG, "getBooleanExtra failed on intent " + th.getMessage());
        }
        return z;
    }

    public static int safeGetIntExtra(Intent intent, String str, int i) {
        try {
            i = intent.getIntExtra(str, i);
        } catch (Throwable th) {
            Log.e(TAG, "getIntExtra failed on intent " + th.getMessage());
        }
        return i;
    }

    public static int safeGetInt(Bundle bundle, String str, int i) {
        try {
            i = bundle.getInt(str, i);
        } catch (Throwable th) {
            Log.e(TAG, "getInt failed on bundle " + th.getMessage());
        }
        return i;
    }

    public static int[] safeGetIntArrayExtra(Intent intent, String str) {
        try {
            return intent.getIntArrayExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getIntArrayExtra failed on intent " + th.getMessage());
            return new int[0];
        }
    }

    public static int[] safeGetIntArray(Bundle bundle, String str) {
        try {
            return bundle.getIntArray(str);
        } catch (Throwable th) {
            Log.e(TAG, "getIntArray failed on bundle " + th.getMessage());
            return new int[0];
        }
    }

    public static long safeGetLongExtra(Intent intent, String str, long j) {
        try {
            j = intent.getLongExtra(str, j);
        } catch (Throwable th) {
            Log.e(TAG, "getLongExtra failed on intent " + th.getMessage());
        }
        return j;
    }

    public static String safeGetStringExtra(Intent intent, String str) {
        try {
            return intent.getStringExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getStringExtra failed on intent " + th.getMessage());
            return null;
        }
    }

    public static String safeGetString(Bundle bundle, String str) {
        try {
            return bundle.getString(str);
        } catch (Throwable th) {
            Log.e(TAG, "getString failed on bundle " + th.getMessage());
            return null;
        }
    }

    public static Bundle safeGetBundleExtra(Intent intent, String str) {
        try {
            return intent.getBundleExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getBundleExtra failed on intent " + th.getMessage());
            return null;
        }
    }

    public static Bundle safeGetBundle(Bundle bundle, String str) {
        try {
            return bundle.getBundle(str);
        } catch (Throwable th) {
            Log.e(TAG, "getBundle failed on bundle " + th.getMessage());
            return null;
        }
    }

    public static <T extends Parcelable> T safeGetParcelable(Bundle bundle, String str) {
        try {
            return bundle.getParcelable(str);
        } catch (Throwable th) {
            Log.e(TAG, "getParcelable failed on bundle " + th.getMessage());
            return null;
        }
    }

    public static <T extends Parcelable> T safeGetParcelableExtra(Intent intent, String str) {
        try {
            return intent.getParcelableExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getParcelableExtra failed on intent " + th.getMessage());
            return null;
        }
    }

    public static <T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(Intent intent, String str) {
        try {
            return intent.getParcelableArrayListExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getParcelableArrayListExtra failed on intent " + th.getMessage());
            return null;
        }
    }

    public static Parcelable[] safeGetParcelableArrayExtra(Intent intent, String str) {
        try {
            return intent.getParcelableArrayExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getParcelableArrayExtra failed on intent " + th.getMessage());
            return new Parcelable[0];
        }
    }

    public static ArrayList<String> safeGetStringArrayListExtra(Intent intent, String str) {
        try {
            return intent.getStringArrayListExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getStringArrayListExtra failed on intent " + th.getMessage());
            return null;
        }
    }

    public static byte[] safeGetByteArrayExtra(Intent intent, String str) {
        try {
            return intent.getByteArrayExtra(str);
        } catch (Throwable th) {
            Log.e(TAG, "getByteArrayExtra failed on intent " + th.getMessage());
            return new byte[0];
        }
    }

    public static <T extends Serializable> T safeGetSerializableExtra(Intent intent, String str) {
        T t = null;
        try {
            t = intent.getSerializableExtra(str);
        } catch (Throwable e) {
            Log.e(TAG, "Invalide class for Serializable: " + str, e);
        } catch (Throwable e2) {
            Log.e(TAG, "getSerializableExtra failed on intent " + e2.getMessage());
        }
        return t;
    }

    public static boolean safeStartActivity(Context context, Intent intent) {
        return safeStartActivity(context, intent, null);
    }

    @TargetApi(16)
    public static boolean safeStartActivity(Context context, Intent intent, Bundle bundle) {
        try {
            context.startActivity(intent, bundle);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    public boolean safeStartActivityForResult(Activity activity, Intent intent, int i) {
        try {
            activity.startActivityForResult(intent, i);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    @RequiresApi(api = 16)
    public boolean safeStartActivityForResult(Activity activity, Intent intent, int i, Bundle bundle) {
        try {
            activity.startActivityForResult(intent, i, bundle);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    public static int getParceledIntentSize(Intent intent) {
        Parcel obtain = Parcel.obtain();
        intent.writeToParcel(obtain, 0);
        int dataSize = obtain.dataSize();
        obtain.recycle();
        return dataSize;
    }

    public static boolean isIntentTooLarge(Intent intent) {
        return getParceledIntentSize(intent) > f;
    }
}
